import { onReady } from "@dcloudio/uni-app"; // uni 专有 Hooks
import { ref } from "vue";

export default function (selector : string) {
	let height = ref(0);
	let width = ref(0);
	let top = ref(0);

	const getElementSize = (selector:string) => {
		uni.createSelectorQuery()
			.select(selector)
			.boundingClientRect((elementInfo:any) => {
				if (!elementInfo) return
				height.value = elementInfo.height;
				width.value = elementInfo.width;
				top.value = elementInfo.top;
			})
			.exec();
	};
	onReady(() => {
		if (!selector) return
		// onReady 触发后才能获取小程序渲染层的节点
		getElementSize(selector);
	});
	return {
		height,
		width,
		top,
		getElementSize,
	};
}
